package cn.edu.scau.cmi.wuzewei.hibernate.client;

import java.util.Iterator;
import java.util.List;

import cn.edu.scau.cmi.wuzewei.hibernate.domain.Student;
import cn.edu.scau.cmi.wuzewei.hibernate.domain.Teacher;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class HibernateDAOClient extends Application {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void start(Stage primaryStage) throws Exception {

		Group group = new Group();

		
		// 学生表
		TableView<Student> stuTable = new TableView<>();
		ObservableList<Student> stuData = FXCollections.observableArrayList();

		List students = HibernateDAOUtil.listStudents();
		Iterator<?> studentIterator = students.iterator();
		while (studentIterator.hasNext()) {
			Student student = (Student) studentIterator.next();
			stuData.add(student);
		}

		stuTable.setEditable(true);
		stuTable.setMaxHeight(200);
		stuTable.setMaxWidth(200);
		stuTable.setLayoutX(20);
		stuTable.setLayoutY(40);

		TableColumn idCol = new TableColumn("学号");
		idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
		TableColumn nameCol = new TableColumn("姓名");
		nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
		TableColumn tutorCol = new TableColumn("导师");
		tutorCol.setCellValueFactory(new PropertyValueFactory<>("teacher"));

		stuTable.setItems(stuData);
		stuTable.getColumns().addAll(idCol, nameCol, tutorCol);

		Label stuLabel = new Label("学生");
		stuLabel.setLayoutX(90);
		stuLabel.setLayoutY(20);
		
		group.getChildren().addAll(stuTable, stuLabel);

		
		// 老师表
		TableView<Teacher> teaTable = new TableView<>();
		ObservableList<Teacher> teaData = FXCollections.observableArrayList();

		List teachers = HibernateDAOUtil.listTeachers();
		Iterator<?> teacherIterator = teachers.iterator();
		while (teacherIterator.hasNext()) {
			Teacher teacher = (Teacher) teacherIterator.next();
			teaData.add(teacher);
		}

		teaTable.setEditable(true);
		teaTable.setMaxHeight(200);
		teaTable.setMaxWidth(200);
		teaTable.setLayoutX(300);
		teaTable.setLayoutY(40);

		TableColumn idTeaCol = new TableColumn("ID");
		idTeaCol.setCellValueFactory(new PropertyValueFactory<>("id"));
		TableColumn nameTeaCol = new TableColumn("姓名");
		nameTeaCol.setCellValueFactory(new PropertyValueFactory<>("name"));
		TableColumn stuTeaCol = new TableColumn("指导学生");
		stuTeaCol.setCellValueFactory(new PropertyValueFactory<>("students"));

		teaTable.setItems(teaData);
		teaTable.getColumns().addAll(idTeaCol, nameTeaCol, stuTeaCol);

		Label teaLabel = new Label("老师");
		teaLabel.setLayoutX(380);
		teaLabel.setLayoutY(20);
		
		group.getChildren().addAll(teaTable, teaLabel);
		
		
		// 添加学生
		TextField addStuIdTextField = new TextField();
		addStuIdTextField.setPromptText("ID");
		addStuIdTextField.setLayoutX(20);
		addStuIdTextField.setLayoutY(250);
		addStuIdTextField.setMaxWidth(60);
		
		TextField addStuNameTextField = new TextField();
		addStuNameTextField.setPromptText("姓名");
		addStuNameTextField.setLayoutX(90);
		addStuNameTextField.setLayoutY(250);
		addStuNameTextField.setMaxWidth(60);
		
		Button addStuBtn = new Button("添加");
		addStuBtn.setLayoutX(170);
		addStuBtn.setLayoutY(250);
		addStuBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Student stu = HibernateDAOUtil.newStudent(Long.valueOf(addStuIdTextField.getText()), addStuNameTextField.getText());
				stuData.add(stu);
			}
		});
		
		group.getChildren().addAll(addStuIdTextField, addStuNameTextField, addStuBtn);

		
		// 添加老师
		TextField addTeaIdTextField = new TextField();
		addTeaIdTextField.setPromptText("ID");
		addTeaIdTextField.setLayoutX(300);
		addTeaIdTextField.setLayoutY(250);
		addTeaIdTextField.setMaxWidth(60);
		
		TextField addTeaNameTextField = new TextField();
		addTeaNameTextField.setPromptText("姓名");
		addTeaNameTextField.setLayoutX(370);
		addTeaNameTextField.setLayoutY(250);
		addTeaNameTextField.setMaxWidth(60);
		
		Button addTeaBtn = new Button("添加");
		addTeaBtn.setLayoutX(450);
		addTeaBtn.setLayoutY(250);
		addTeaBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Teacher tea = HibernateDAOUtil.newTeacher(Long.valueOf(addTeaIdTextField.getText()), addTeaNameTextField.getText());
				teaData.add(tea);
			}
		});
		
		group.getChildren().addAll(addTeaIdTextField, addTeaNameTextField, addTeaBtn);
		
		
		// 删除学生
		Button delStuBtn = new Button("删除学生");
		delStuBtn.setLayoutX(230);
		delStuBtn.setLayoutY(210);
		delStuBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Student student = stuTable.getSelectionModel().getSelectedItem();
				HibernateDAOUtil.delStudent(student);
				stuData.remove(student);
			}
		});
		
		group.getChildren().addAll(delStuBtn);
		
		
		// 删除老师
		Button delTeaBtn = new Button("删除老师");
		delTeaBtn.setLayoutX(510);
		delTeaBtn.setLayoutY(210);
		delTeaBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Teacher teacher = teaTable.getSelectionModel().getSelectedItem();
				HibernateDAOUtil.delTeacher(teacher);
				teaData.remove(teacher);
			}
		});
		
		group.getChildren().addAll(delTeaBtn);
		
		
		// 修改学生姓名
		TextField modStuTextField = new TextField();
		modStuTextField.setLayoutX(20);
		modStuTextField.setLayoutY(280);
		
		Button modStuBtn = new Button("修改姓名");
		modStuBtn.setLayoutX(170);
		modStuBtn.setLayoutY(280);
		modStuBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Student student = stuTable.getSelectionModel().getSelectedItem();
				student.setName(modStuTextField.getText());
				HibernateDAOUtil.modStudent(student);
				
				stuTable.refresh();
			}
		});
		
		group.getChildren().addAll(modStuTextField, modStuBtn);
		
		
		// 修改老师姓名
		TextField modTeaTextField = new TextField();
		modTeaTextField.setLayoutX(300);
		modTeaTextField.setLayoutY(280);
		
		Button modTeaBtn = new Button("修改姓名");
		modTeaBtn.setLayoutX(450);
		modTeaBtn.setLayoutY(280);
		modTeaBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Teacher teacher = teaTable.getSelectionModel().getSelectedItem();
				teacher.setName(modTeaTextField.getText());
				HibernateDAOUtil.modTeacher(teacher);
				
				teaTable.refresh();
			}
		});
		
		group.getChildren().addAll(modTeaTextField, modTeaBtn);
		
		
		// 修改指导老师
		Button modTutorBtn = new Button("修改导师");
		modTutorBtn.setLayoutX(170);
		modTutorBtn.setLayoutY(320);
		modTutorBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Student student = stuTable.getSelectionModel().getSelectedItem();
				Teacher teacher = teaTable.getSelectionModel().getSelectedItem();
				student.setTeacher(teacher);
				HibernateDAOUtil.modStudent(student);
				
				teaData.clear();
				List teachers = HibernateDAOUtil.listTeachers();
				Iterator<?> teacherIterator = teachers.iterator();
				while (teacherIterator.hasNext()) {
					Teacher tea = (Teacher) teacherIterator.next();
					teaData.add(tea);
				}
				
				stuTable.refresh();
				teaTable.refresh();
			}
		});
		
		Label modTutorLabel = new Label("请在右边选中导师后点击：");
		modTutorLabel.setLayoutX(20);
		modTutorLabel.setLayoutY(320);
		
		group.getChildren().addAll(modTutorBtn, modTutorLabel);
		
		
		
		Scene scene = new Scene(group);
		primaryStage.setTitle("实验七：Hibernate框架");
		primaryStage.setWidth(600);
		primaryStage.setHeight(400);
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void main(String[] args) {
		launch(args);
	}
}
